
function getCookieValue(name) {
    var cookieValue = "";
    var search = name + "=";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) {
                end = document.cookie.length;
            }
            cookieValue = unescape(document.cookie.substring(offset, end));
        }
    }
    return cookieValue;
}

function setCookies(name, value, time)
{
	var cookieString = name + "=" + escape(value) + ";";
	if (time != 0) {
		var Times = new Date();
		Times.setTime(Times.getTime() + time);
		cookieString += "expires="+Times.toGMTString()+";"
	}
	document.cookie=cookieString;
}

function getRadioValue(radioName) {
    var radio = document.getElementsByName(radioName);
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return radio[i].value;
        }
    }
    return "";
}

function selectAllCheckBox(id, selectChoiceName, saveValueElementID){
	var choices = $("input[name='" + selectChoiceName + "']");
	var topSelect = document.getElementById(id).checked;
    for (var i = 0; i < choices.length; i++) {
        choices[i].checked = topSelect;
    }
    setSelectedValues(selectChoiceName, saveValueElementID);
}

function selectAllCheckBox2(id, id2, selectChoiceName, saveValueElementID){
	var choices = $("input[name='" + selectChoiceName + "']");
	var topSelect = document.getElementById(id).checked;
	document.getElementById(id2).checked = topSelect;
    for (var i = 0; i < choices.length; i++) {
        choices[i].checked = topSelect;
    }
    setSelectedValues(selectChoiceName, saveValueElementID);
}

function setSelectedValues(selectChoiceName, saveValueElementID) {
    var valueString = "";
    var choices = document.getElementsByName(selectChoiceName);
    for (var i = 0; i < choices.length; i++) {
        if (choices[i].checked) {
            valueString += choices[i].value + ",";
        }
    }
    valueString = valueString.substring(0, valueString.length - 1);
    document.getElementById(saveValueElementID).value = valueString;
}

function calendar(object) {
    WdatePicker({onpicking:function () {
        object.focus();
    }, skin:"whyGreen", minDate:""});
}

function showDiv(name) {
    document.getElementById(name).style.display = 'block';
}

function hideDiv(name) {
    document.getElementById(name).style.display = 'none';
    
}

function checkUploadImageFile(pathField)
{
    var imgRe = /^.+\.(jpg|jpeg|gif|png)$/i;
    var path = pathField.value;
    
    if (path.search(imgRe) == -1) {
        alert("请上传格式为JPG、PNG或者GIF的图片!");
        return;
    }
    
    return true;
}

function jumpPage(pageIndex, max) {
    if (pageIndex < 1 || pageIndex > max) {
        return;
    }
    document.getElementById("pageIndex").value=pageIndex;
    doJumpPage();
}

function ajaxJumpPage(pageIndex, max) {
     if (pageIndex < 1 || pageIndex > max) {
        return;
    }
    document.getElementById("pageIndex").value=pageIndex;
    refreshItem('productCommentInfo');
}

function jumpToPage() {
    var jumpToPageIndex = document.getElementById("pageIndexInput").value;
    var jumpToPageIndexValue = parseInt(jumpToPageIndex);
    if (!jumpToPageIndexValue) {
        alert("请输入一个正确的数值");
        document.getElementById("pageIndexInput").focus();
        return;
    }
    var jumpToPageMaxValue = parseInt(document.getElementById("pageCount").value);
    if (jumpToPageIndex < 1 || jumpToPageIndex > jumpToPageMaxValue) {
        alert("请输入一个介于1和" + jumpToPageMaxValue + "的数值");
        document.getElementById("pageIndexInput").focus();
        return;
    }
    document.getElementById("pageIndex").value=jumpToPageIndexValue;
    doJumpPage();
}

function dispatch(link) {
    location.href = link;
}

function closeInfoWindow() {
    document.getElementById("windowInsideDIV").innerHTML = "";
    hideWindow("infoWindow");
}

function checkContent(contentID) {
    document.getElementById(contentID).value = getEditorHTMLContents(contentID);
    if (document.getElementById(contentID).value.length > 65535) {
        alert("内容过长，请控制在10000字以内");
        return false;
    }
    else if (document.getElementById(contentID).value.match("<script")) {
        alert("内容中含有不安全的脚本代码，请检查您的内容");
        return false;
    }
    return true;
}

function clearFiles() {
    var nodeArray = document.getElementsByTagName("input");
    for(i=0;i<nodeArray.length;i++) {
        var tmpobj = nodeArray[i];
        var stype = tmpobj.type;
        if (stype == "file") {
            tmpobj.outerHTML=tmpobj.outerHTML;
        }
    }
}

function changeRandomImage() {
    document.getElementById('randomNumberImage').src='/randomNumberImage?temp='+(new Date().getTime().toString(36));
}

function checkImage(pathField) {
    var imgRe = /^.+\.(jpg|jpeg|gif|png)$/i;
    var path = pathField.value;
    
    if (path.search(imgRe) == -1) {
        alert("请上传格式为JPG、PNG或者GIF的图片!");
        return;
    }
    
    return true;
}

function setCoordination() {
    document.getElementById("mask").style.height = document.documentElement.clientHeight + "px";
    document.getElementById("mask").style.width = document.documentElement.clientWidth + "px";
}

function showWindow(displayedID) {
    var displayedWindow = document.getElementById(displayedID);
    var height = parseInt(displayedWindow.clientHeight);
    var width = parseInt(displayedWindow.clientWidth);
    var position = getPosition();
    var topAdded = (position.height - height) / 2;
    var leftAdded = (position.width - width) / 2;
    
    // displayedWindow.style.top = ( position.top + topAdded - 46) + "px";
    displayedWindow.style.top = ( position.top + topAdded) + "px";
    displayedWindow.style.left = ( position.left + leftAdded ) + "px";
    displayedWindow.style.visibility = "visible";
    
    var mask = document.getElementById("mask");
    $('#mask').css('height', $(document).height());  //遮罩层覆盖全屏
    mask.style.visibility = "visible";
}

function hideWindow(hiddenID) {
    var hiddenWindow = document.getElementById(hiddenID);
    hiddenWindow.style.visibility = "hidden";
    var mask = document.getElementById("mask");
    mask.style.visibility = "hidden";
}

function getPosition() {
    var top    = 0;
    var left   = 0;
    var height = 0;
    var width  = 0;
    if (document.compatMode == "\"BackCompat\"") {
		width =document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
		left = document.documentElement.scrollLeft;
		top = document.documentElement.scrollTop;
	}
	else { 
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
		left = document.documentElement.scrollLeft == 0 ? document.body.scrollLeft : document.documentElement.scrollLeft;
		top = document.documentElement.scrollTop == 0 ? document.body.scrollTop : document.documentElement.scrollTop;
	} 
    
	return {top:top,left:left,height:height,width:width};
}

/***
*显示错误信息
**/
function showErrorMsg(eleid, errorMsg) {
	var error_label_id = "msg_msg_" + eleid;
	if (document.getElementById(error_label_id)) {
		$('#' + error_label_id).html(errorMsg);
	} else {
		$('#' + eleid).after("<label class='frame_msg' id='" + error_label_id + "'>" + errorMsg + "</label>");
	}
	
	if (errorMsg == '') {
		hideError(eleid);
	} else {
		$('#' + error_label_id).show();
	}
}

/**将锚点定位到第一个错误信息**/
function gotoTopMsg() {
	var maodian = $(".frame_msg:visible:first").attr('id');
	if (maodian != '') {
		var top = $('#' + maodian).offset().top;
		if (top > 30) {
			top = top - 30;
		}
		$("html,body").animate({ scrollTop : top }, "slow");
	} 
}

/***
*隐藏错误信息
**/
function hideErrorMsg(eleid) {
	var error_label_id = "msg_msg_" + eleid;
	$('#' + error_label_id).hide();
}

function hideAllErrorMsg() {
	$('.frame_msg').hide();
}

function bindHideErrorMsgEvent() {
	$(":text,:password,:radio,:checkbox").each(function(i){
		var id = $($(":text,:password,:radio,:checkbox")[i]).attr('id');
		$($(":text,:password,:radio,:checkbox")[i]).focus(function() {
			hideErrorMsg(id);
		});
	});
	
	$("textarea").each(function(i){
		var id = $($("textarea")[i]).attr('id');
		$($("textarea")[i]).focus(function() {
			hideErrorMsg(id);
		});
	});
	
	$("select").each(function(i){
		var id = $($("select")[i]).attr('id');
		$($("select")[i]).change(function() {
			hideErrorMsg(id);
		});
	});
}

function getSelectValue(selecteID) {
	var selectionObject = document.getElementById(selecteID);
    for (var i = 0; i < selectionObject.options.length; i++) {
        if (selectionObject.options[i].selected == true) {
            return selectionObject.options[i].value;
        }
    }
    
    return "";
}

function ajaxJumpPageComment(pageIndex, max) {
    if (pageIndex < 1 || pageIndex > max) {
        return;
    }
    document.getElementById("commentPageIndex").value=pageIndex;
    refreshItem('product', 'refreshProductComment', 'productCommentInfo');
}

function ajaxJumpPageConsu(pageIndex, max) {
    if (pageIndex < 1 || pageIndex > max) {
        return;
    }
    document.getElementById("consuPageIndex").value=pageIndex;
    refreshItem('product', 'refreshProductConsulation', 'productConsultInfo');
}

/**
 * 遍历所有元素，如果某个元素样式中的
 * @param contentID
 * @param maxWidth
 */
function dealContent(contentID, maxWidth) {
		$('#' + contentID + ' *').each(function(i){
			var width = $($('#' + contentID + ' *')[i]).css('width').replace('px', '').replace('em', '').replace('ex', '').replace('in', '').replace('cm', '').replace('mm', '').replace('pt', '').replace('pc', '');
			try {
				if (width > maxWidth) {
					$($('#' + contentID + ' *')[i]).css('width', '');
				}
			} catch (e){}
			
		});
}


function doUploadFile(toDir, fileNameHolder, srcHolder, uploadItemType) {
	$('#uploadDir').val(toDir);
	$('#uploadItemType').val(uploadItemType);
	$('#imageNameHolderID').val(fileNameHolder);
	$('#imageSrcHolderID').val(srcHolder);
	openInfoWindow('uploadImageWindow');
}

function clearUploadFile(fileNameHolder, srcHolder) {
	$('#' + fileNameHolder).val('');
	$('#' + srcHolder).attr('src', '/images/none.jpg');
}

function confirmUploadImage() {
	var previewImg_window = $('#previewImg_window').attr('src');
	var tmpImage_window = $('#tmpImage_window').val();
	if (tmpImage_window == '') {
		alert('请先上传一张图片');
		return;
	}
	
	
	var imageNameHolderID = $('#imageNameHolderID').val();
	var imageSrcHolderID = $('#imageSrcHolderID').val();
	$('#' + imageSrcHolderID).attr('src', previewImg_window);
	$('#' + imageNameHolderID).val(tmpImage_window);
	closeInfoWindow();
}